package com.zby.demo.controller.front;

import com.zby.demo.dao.ClassDao;
import com.zby.demo.dao.CourseDao;
import com.zby.demo.dao.NoticeDao;
import com.zby.demo.dao.StudentDao;
import com.zby.demo.entity.*;
import com.zby.demo.entity.Class;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreateDate 2020/12/13 18:15
 */

@Controller
public class StuController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    NoticeDao noticeDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    ClassDao classDao;

    /*
     * @Param: [model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/18 18:21
     * @Description: 转发到前台主页
     */
    @RequestMapping("/front/student")
    public String toFrontIndex(Model model) {
        List<Notice> notices = noticeDao.queryAllNotice();
        model.addAttribute("notices", notices);
        return "front/student/index";
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 21:42
     * @Description: 转发到前台选课页面。
     */
    @RequestMapping({"/front/student/selectcourse"})
    public String toSelectCourse(HttpSession session, Model model) {
        List<Course> list = newList(courseDao.queryAllCourse());
        model.addAttribute("courses", list);
        List<Course> selectCourses = newList(studentDao.querySelectCourseByStudentID((String) session.getAttribute("LoginUser")));
        model.addAttribute("selectCourses", selectCourses);
        String msg = (String) session.getAttribute("msg");
        if (msg != null) {
            model.addAttribute("msg", msg);
            session.removeAttribute("msg");
        }
        return "front/student/selectcourse";
    }

    /*
     * @Param:
     * @Return:
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:10
     * @Description: 学生选课操作
     */
    @GetMapping("/front/student/selectcourse/{courseId}")
    public String selectCourse(@PathVariable("courseId") String courseId, HttpSession session) {
        String loginUser = (String) session.getAttribute("LoginUser");
        List<Course> selectCourses = studentDao.querySelectCourseByStudentID(loginUser);
        for (Course course : selectCourses) {
            if (course.getCourseId().equals(courseId)) {
                session.setAttribute("msg", "选课失败，您已选报此课程，请勿重复选报");
                return "redirect:/front/student/selectcourse";
            }
        }
        studentDao.setSelectCourse(loginUser, courseId);
        session.setAttribute("msg", "选课成功，请注意课程安排");
        return "redirect:/front/student/selectcourse";
    }

    /*
     * @Param: [courseId, session]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:13
     * @Description: 学生退课操作
     */
    @GetMapping("/front/student/selectcourse/delete/{courseId}")
    public String deleteSelectCourse(@PathVariable("courseId") String courseId, HttpSession session) {
        String loginUser = (String) session.getAttribute("LoginUser");
        studentDao.deleteSelectCourse(loginUser, courseId);
        session.setAttribute("msg", "退选成功，请注意查看");
        return "redirect:/front/student/selectcourse";
    }

    //更新课程状态
    List<Course> newList(List<Course> courses) {
        List<Course> list = new ArrayList<>();
        long time = new Date().getTime();
        for (Course course : courses) {
            if (time - course.getBeginDate().getTime() >= 0) {
                if (course.getEndDate().getTime() + 1000 * 60 * 60 * 24 > time) {
                    course.setStatus("进行中");
                    list.add(0, course);
                } else {
                    course.setStatus("已结束");
                    list.add(course);
                }
            } else {
                course.setStatus("未开始");
                list.add(course);
            }
        }
        return list;
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:13
     * @Description: 前台学生成绩查看
     */
    @RequestMapping("/front/student/scoreview")
    public String toScoreView(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        List<StudentSelectCourses> selectcourses = studentDao.selectcourses();
        for (StudentSelectCourses ssc : selectcourses) {
            if (ssc.getStudent().getStudentId().equals(loginUser)) {
                model.addAttribute("StudentSelectCourses", ssc.getStudentSelectCourses());
                return "front/student/scoreview";
            }
        }
        return "front/student/scoreview";
    }

    /*
     * @Param: [session, model]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:14
     * @Description: 学生信息页面
     */
    @RequestMapping("/front/student/info")
    public String toInfo(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        Student student = studentDao.queryStudentByUsername(loginUser);
        model.addAttribute("stu", student);
        List<Class> classes = classDao.queryAllClass();
        model.addAttribute("classes", classes);
        return "front/student/info";
    }

    /*
     * @Param: [student]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:15
     * @Description: 修改学生信息
     */
    @PostMapping("/front/student/info")
    public String updateInfo(Student student) {
        studentDao.updateStudentInfo(student);
        return "redirect:/front/student/info";
    }

    /*
     * @Param: [studentId, password]
     * @Return: java.lang.String
     * @Author: ChrisPhantom
     * @Date: 2020/12/23 22:15
     * @Description: 修改密码
     */
    @PostMapping("/front/student/info/{studentId}")
    public String updatePassword(@PathVariable("studentId") String studentId, @Param("password") String password) {
        studentDao.updateStudentPassword(studentId, password);
        return "redirect:/logout";
    }
}
